using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data;

using WP.Commons;
using WP.DB;
using WP.MF.DataAccess;
using WP.MF.DataEntity;

namespace WP.MF.BusinessRule
{
    public class MSwitchBRL : RuleBase
    {
        public MSwitchBRL()
		{
			//
			// TODO: Add constructor logic here
			//
		}
        #region Insert MSwitch Valid
        public bool InsertValid(DBAccess dBAccess, MSwitchDEL mSwitchDEL)
        {
            bool bReturn = true;
            SysVar.ErrorMessage.Clear();
            string strErrPrompt = string.Empty;
            DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MSwitchDEL.TABLENAME_MSWITCH + "'").DefaultView;
            foreach (DataRowView drw in dvwCheck)
            {
                if (!FieldCheck(dBAccess, mSwitchDEL.Tables[MSwitchDEL.TABLENAME_MSWITCH].Rows[0], drw["FieldName"].ToString(), ref  strErrPrompt, FormStatus.AddNew))
                {
                    if (SysVar.ErrorMessage.Count == 0)
                    {
                        SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString().TrimEnd()));
                    }
                    bReturn = false;
                    break;
                }
            }
            return bReturn;
        }
        #endregion
        #region Update MSwitch Valid
        public bool UpdateValid(DBAccess dBAccess, MSwitchDEL mSwitchDEL)
        {
            bool bReturn = true;
            SysVar.ErrorMessage.Clear();
            string strErrPrompt = string.Empty;
            if (IsExistedAndNotChanged(dBAccess, mSwitchDEL.Tables[MSwitchDEL.TABLENAME_MSWITCH]))
            {
                DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MSwitchDEL.TABLENAME_MSWITCH + "'").DefaultView;

                foreach (DataRowView drw in dvwCheck)
                {
                    if (!FieldCheck(dBAccess, mSwitchDEL.Tables[MSwitchDEL.TABLENAME_MSWITCH].Rows[0], drw["FieldName"].ToString(), ref  strErrPrompt, FormStatus.Update))
                    {
                        bReturn = false;
                        if (SysVar.ErrorMessage.Count == 0)
                        {
                            SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                        }
                        break;
                    }
                }
            }
            else
            {
                bReturn = false;
            }
            return bReturn;
        }
        #endregion
        #region Delete MSwitch Valid
        public bool DeleteValid(DBAccess dBAccess, string strRecId, byte[] btOldRecStamp)
        {
            SysVar.ErrorMessage.Clear();
            if (!base.IsExistedAndNotChanged(dBAccess, "MSwitch", strRecId, btOldRecStamp))
            {
                return false;
            }
            if (MsgBoxEx.Show(Msg.Select("Are you sure to delete this Switch?", DataFunctions.GetMsg(this.GetType().Name, "MSG_DeleteThisSwitch")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return false;
            }
            return true;
        }
        #endregion
        public bool FieldCheck(DBAccess dBAccess, DataRow dr, string strFieldName, ref string strErrPrompt, FormStatus formStatus)
        {
            strErrPrompt = "";
            bool blnExistsField = false;
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].Caption.ToUpper().Trim() == strFieldName.Trim().ToUpper())
                {
                    blnExistsField = true;
                    break;
                }
            }
            if (!blnExistsField) return true;
            string strFieldValue = CvtFunc.ToString(dr[strFieldName.Trim()]);
            switch (strFieldName.ToUpper().Trim())
            {
                #region SWITCHNAME
                case "SWITCHNAME":
                    if (formStatus == FormStatus.AddNew)
                    {
                        if (DataFunctions.CheckRecordExist(dBAccess, "Mswitch", strFieldValue, strFieldName))
                        {
                            strErrPrompt =Msg.Select("Switch Name already on file!", DataFunctions.GetMsg(this.GetType().Name, "MSG_SwitchNameAlreadyOnFile"));
                            return false;
                        }
                        else
                            return true;
                    }
                    else
                        return true;
                #endregion
                default:
                    return RuleBase.FieldValueCheckOne(dBAccess, strFieldValue, "MMiscSwitch", strFieldName, ref strErrPrompt);
            }
        }
    }
}
